<template>
    <article class="article-content markdown-body">
      <div class="article-header">
        <h1>{{ title }}</h1>
        <div class="article-meta">
          <div class="meta-left">
            <img :src="author.avatarUrl || '/default-avatar.png'" :alt="author.nickname" class="author-avatar">
            <span class="author-name">{{ author.nickname }}</span>
            <span class="publish-time">{{ formatDate(publishedAt) }}</span>
          </div>
          <div class="meta-right">
            <span class="meta-item"><i class="far fa-eye"></i> {{ viewCount }}</span>
            <span class="meta-item"><i class="far fa-heart"></i> {{ likeCount }}</span>
            <span class="meta-item"><i class="far fa-comment"></i> {{ commentCount }}</span>
          </div>
        </div>
      </div>
      <div class="article-body" v-html="formattedContent"></div>
    </article>
  </template>
  
  <script>
  import { marked } from 'marked'
  import DOMPurify from 'dompurify'
  
  export default {
    name: 'ArticleContent',
    props: {
      title: String,
      content: String,
      author: Object,
      publishedAt: String,
      viewCount: Number,
      likeCount: Number,
      commentCount: Number
    },
    computed: {
      formattedContent() {
        // 自定义渲染器来处理图片
        const renderer = new marked.Renderer()
        renderer.image = (href, title, text) => {
          return `<div class="image-container"><img src="${href}" alt="${text}" title="${title || ''}" /></div>`
        }
        
        // 设置marked选项
        marked.setOptions({
          renderer,
          gfm: true,
          breaks: true,
          indent: true
        })
        
        // 渲染并净化HTML
        const rawHtml = marked(this.content)
        return DOMPurify.sanitize(rawHtml)
      }
    },
    methods: {
      formatDate(date) {
        return new Date(date).toLocaleDateString('zh-CN', {
          year: 'numeric',
          month: 'long',
          day: 'numeric'
        })
      }
    }
  }
  </script>
  
  <style>
  .article-content {
    max-width: 860px;
    margin: 0 auto;
    padding: 40px 20px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  }
  
  .article-header {
    margin-bottom: 40px;
    text-align: center;
  }
  
  .article-header h1 {
    font-size: 2.5em;
    font-weight: 700;
    margin-bottom: 20px;
    color: var(--text-primary);
  }
  
  .article-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 0;
    border-bottom: 1px solid var(--border-color);
  }
  
  .meta-left {
    display: flex;
    align-items: center;
    gap: 12px;
  }
  
  .author-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
  }
  
  .author-name {
    font-weight: 600;
    color: var(--text-primary);
  }
  
  .publish-time {
    color: var(--text-secondary);
  }
  
  .meta-right {
    display: flex;
    gap: 20px;
  }
  
  .meta-item {
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 6px;
  }
  
  /* Markdown 内容样式 */
  .article-body {
    font-size: 16px;
    line-height: 1.8;
    color: var(--text-primary);
  }
  
  .article-body h2 {
    font-size: 1.75em;
    margin: 2em 0 1em;
    padding-bottom: 0.3em;
    border-bottom: 1px solid var(--border-color);
  }
  
  .article-body h3 {
    font-size: 1.5em;
    margin: 1.5em 0 1em;
  }
  
  .article-body p {
    margin: 1em 0;
    text-indent: 2em;
  }
  
  .article-body ul, .article-body ol {
    padding-left: 2em;
    margin: 1em 0;
  }
  
  .article-body li {
    margin: 0.5em 0;
  }
  
  /* 图片容器样式 */
  .image-container {
    text-align: center;
    margin: 2em 0;
  }
  
  .image-container img {
    max-width: 100%;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
  
  /* 代码块样式 */
  .article-body pre {
    background: var(--code-bg);
    border-radius: 8px;
    padding: 16px;
    overflow-x: auto;
    margin: 1.5em 0;
  }
  
  .article-body code {
    font-family: 'Fira Code', monospace;
    font-size: 0.9em;
  }
  
  /* 暗色模式适配 */
  .dark-mode .article-body {
    color: var(--text-dark-primary);
  }
  
  .dark-mode .article-header h1 {
    color: var(--text-dark-primary);
  }
  
  .dark-mode .article-meta {
    border-color: var(--border-dark);
  }
  
  .dark-mode .meta-item {
    color: var(--text-dark-secondary);
  }
  
  .dark-mode .image-container img {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }
  
  @media (max-width: 768px) {
    .article-content {
      padding: 20px 16px;
    }
    
    .article-header h1 {
      font-size: 2em;
    }
    
    .meta-right {
      display: none;
    }
  }
  /* Markdown 内容样式 */
.article-body {
  font-size: 16px;
  line-height: 1.8;
  color: var(--text-primary);
  text-align: left; /* 确保左对齐 */
}

.article-body p {
  margin: 1em 0;
  text-indent: 2em; /* 首行缩进2个字符 */
  text-align: left;
}

/* 标题样式左对齐 */
.article-body h2,
.article-body h3,
.article-body h4,
.article-body h5,
.article-body h6 {
  text-align: left;
  text-indent: 0; /* 标题不需要缩进 */
}

/* 列表项左对齐且不缩进 */
.article-body ul,
.article-body ol {
  text-align: left;
  text-indent: 0;
  padding-left: 2em;
  margin: 1em 0;
}

.article-body li {
  text-indent: 0;
  margin: 0.5em 0;
}

/* 代码块左对齐且不缩进 */
.article-body pre {
  text-indent: 0;
  text-align: left;
}

/* 只有图片保持居中 */
.image-container {
  text-align: center;
  text-indent: 0;
  margin: 2em 0;
}
  </style>